
package pe.edu.pucp.cpnc.mm.view;

import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import pe.edu.pucp.cpnc.mm.bean.Map;
import pe.edu.pucp.cpnc.mm.renderer.CameraPanController;
import pe.edu.pucp.cpnc.mm.renderer.Renderer;

/**
 *
 * @author Luis
 */
public class MinimapPanel extends JPanel implements MouseListener, FocusListener {
    private final MainFrame mf;
    private boolean mousePressed;
    
    public MinimapPanel(MainFrame mf){
        setSize(300, 300);
        this.mf = mf;
        this.mousePressed=false;
        this.setFocusable(true);
        this.addMouseListener(this);
    }
    
    public void updateCamera(){
        if(mousePressed){
            Point point = MouseInfo.getPointerInfo().getLocation();
            Point point2;
            try{point2 = getLocationOnScreen();}
            catch(Exception e){point2 = new Point(0, 0);}

            double mouseX = point.getX() - point2.getX();
            double mouseY = point.getY() - point2.getY();
            
            Map map = mf.getMap();
            int mapWidth= (map.getSizeX() + map.getSizeY())*Renderer.Anchura/2;
            double scale = (double)this.getWidth()/mapWidth;
            CameraPanController.minimapPanEvent((int)(mouseX/scale) - mf.getWorkingArea().getWidth()/2
                    ,(int)(mouseY/scale) - mf.getWorkingArea().getHeight()/2);
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        long t1 = System.currentTimeMillis();
        super.paintComponent(g);
        BufferedImage minimap = mf.rd.minimap;
        if(mf.getMap()!=null && minimap!=null){
            mf.rd.updateMinimapOverlay(mf.getMap(), mf.getWorkingArea());
            g.drawImage(minimap, 0,0,this.getWidth(), this.getHeight(), this);
        }
        System.out.println(System.currentTimeMillis()-t1);
    }

    @Override
    public void mouseClicked(MouseEvent e) {}

    @Override
    public void mousePressed(MouseEvent e) {
        mousePressed = true;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        mousePressed = false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}

    @Override
    public void focusGained(FocusEvent e) {}

    @Override
    public void focusLost(FocusEvent e) {
        mousePressed = false;
    }
    
    
    
}
